package com.zgw.his.design.common;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;

public class EditTable extends Table {
	private TableCursor cursor;
	private ControlEditor editor;
	private Text text;
	protected int rowHeight = 25;
	private boolean dirty = false;
	protected TableItem row;

	private static final Color topColor = RcpResourceManager.getColor(236, 233,
			216);
	private static final Color textColor = RcpResourceManager.getColor(255,
			250, 205);

	public EditTable(Composite parent, int style) {
		super(parent, style);
		createControls();
		initializeListener();
	}

	private void createControls() {
		cursor = new TableCursor(this, SWT.NONE);
		editor = new ControlEditor(cursor);
		editor.grabHorizontal = true;
		editor.grabVertical = true;
		text = new Text(cursor, SWT.NONE);
		text.setBackground(textColor);
		editor.setEditor(text);
		{
			TableColumn column = new TableColumn(this, SWT.NONE);
			column.setWidth(30);
			column.setResizable(false);
			column.setAlignment(SWT.LEFT_TO_RIGHT);
		}
	}

	private void initializeListener() {
		cursor.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				doSelectedCursor();
			}
		});
		this.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				int width = 0;
				for (TableColumn c : getColumns()) {
					width += c.getWidth();
				}
				if (e.x > 30 && e.x < width) {
					setCursor(SWTResourceManager.getCursor(SWT.CURSOR_IBEAM));
				} else {
					setCursor(null);
				}
			}
		});
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				TableItem row = cursor.getRow();
				int column = cursor.getColumn();
				row.setText(column, text.getText());
				dirty = true;
			}
		});
		addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {
				event.height = rowHeight;
			}
		});
	}

	protected void doSelectedCursor() {
		if (row != null && !row.isDisposed()) {
			row.setText(0, "");
		}
		row = cursor.getRow();
		setSelection(row);
		int column = cursor.getColumn();
		if (column != 0) {
			text.setText(row.getText(column));
			text.setSelection(text.getText().length());
			text.setFont(row.getFont());
		}
		text.setVisible(column != 0);
		row.setText(0, "▲");
	}

	public void setSelection(TableItem item, int column) {
		cursor.setSelection(item, column);
		doSelectedCursor();
	}

	public boolean isDirty() {
		return dirty;
	}

	@Override
	protected void checkSubclass() {
	}

	public void setRowHeight(int height) {
		this.rowHeight = height;
	}

}
